/*
 * RecipeClient.java
 *
 * Created on 19 de julio de 2007, 04:54
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ws;

import javax.xml.soap.SOAPMessage;

/**
 *
 * @author mariano
 */
public class RecipeClient extends P2PClient {
    
    /** Creates a new instance of RecipeClient */
    public RecipeClient( String url, String serviceName, String portName, String nameSpace, String uri ){
        super(url, serviceName, portName, nameSpace, uri);
        // parameter example:
        // "http://localhost:8765/", "Fresitaws", "FresitaPort", "http://localhost:8765/", "http://fresita/"
    }
    
    public model.Recipe getRecipe(String path)
    {
        client = new soap.Client(url, serviceName, portName, nameSpace);
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "getRecipe", "s", uri );
        method.addParameter(new soap.SoapParameter("arg0",path));
        
        try
        {
            message.addMethod(method);
            SOAPMessage response = client.invoke(message.toString());
            if(response!=null)
                response.writeTo( System.err );
            return model.Recipe.fromXml( soap.Client.getMessageResponse( response, "return" ) );
        }
        catch( Exception ex )
        {
            System.err.println( "error" + ex.toString() );
            ex.printStackTrace();
            return null;
        }
    }
    
    public model.Ingredient getIngredient(String path)
    {
        client = new soap.Client(url, serviceName, portName, nameSpace);
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "getIngredient", "s", uri );
        method.addParameter(new soap.SoapParameter("arg0",path));
        
        try
        {
            message.addMethod(method);
            SOAPMessage response = client.invoke(message.toString());
            if(response!=null)
                response.writeTo( System.err );
            return model.Ingredient.fromXml( soap.Client.getMessageResponse( response, "return" ) );
        }
        catch( Exception ex )
        {
            System.err.println( "error" + ex.toString() );
            ex.printStackTrace();
            return null;
        }
    }
    
    public boolean addIngredient(model.Ingredient ingredient)
    {
        client = new soap.Client(url, serviceName, portName, nameSpace);
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "addIngredient", "s", uri );
        method.addParameter(new soap.SoapParameter("arg0",ingredient.toXmlString()));
        SOAPMessage response = null;
        
        try
        {
            message.addMethod(method);
            response = client.invoke(message.toString());
            if(response!=null)
                response.writeTo( System.err );
            return soap.Client.getMessageResponse( response, "return" ).equals("true");
        }
        catch( Exception ex )
        {
            System.err.println( "error" + ex.toString() );
            ex.printStackTrace();
            return false;
        }
    }
    
    public boolean addRecipe(model.Recipe recipe)
    {
        client = new soap.Client(url, serviceName, portName, nameSpace);
        soap.SoapMessage message = new soap.SoapMessage();
        soap.SoapMethod method = new soap.SoapMethod( "addRecipe", "s", uri );
        method.addParameter(new soap.SoapParameter("arg0",recipe.toXmlString()));
        SOAPMessage response = null;
        
        try
        {
            message.addMethod(method);
            response = client.invoke(message.toString());
            if(response!=null)
                response.writeTo( System.err );
            return soap.Client.getMessageResponse( response, "return" ).equals("true");
        }
        catch( Exception ex )
        {
            System.err.println( "error " + ex.toString() );
            ex.printStackTrace();
            return false;
        }
    }
    
}
